function EJGetDietByWeek(personID, dateString) {
    // Disable "update" button and clean
    W3DisableUI("uidDietUpdateButton");
    W3SetUIText("uidInvisibleDate", "");
    W3SetUIText("uidInvisibleOwner", "");

    // Validate datetime format "YYYY-MM-DD"
    var pattern = new RegExp("^([0-9]{4})-([0-9]{2})-([0-9]{2})$");
    var result = pattern.exec(dateString);
    if (result == null) {
	W3LogError("It's not W3 datetime format (YYY-MM-DD): " + dateString);
	return;
    }

    // Calculate week days
    currentDate = new Date(result[1], parseInt(result[2] - 1), result[3]);
    var currentDay = currentDate.getDay();
    var weekDays = [];
    for (var offset = currentDay; currentDay - offset < 7; --offset) {
        var date = new Date(currentDate);
        date.setDate(currentDate.getDate() - offset);
        weekDays.push(date);
    }
    if (weekDays.length != 7) {
	W3LogError("Wrong algrithm for week days calculation!");
	return;
    }

    // Call diet API for week days
    var dietArray = [];
    for (var i = 0; i < weekDays.length; ++i) {
        var aid = "aidDiet";
        var dateString = String(weekDays[i].getFullYear()) + "-" +
            String(weekDays[i].getMonth() + 1) + "-" +
            String(weekDays[i].getDate());
        var request = W3CreateAPI(aid, personID, dateString, W3GetSession());
        W3CallAPISync(request, function(data, status) {
            var diet = 0;
            var apiResult = eval("(" + data + ")");
            if (apiResult[w3ApiResultStatus] == w3ApiResultSuccessful) {
                if (apiResult[w3ApiResultData].length > 0) {
                    diet = apiResult[w3ApiResultData][0]["diet"];
                }
            }

            dietArray.push(diet);
        });
    }
    if (dietArray.length != 7) {
	W3LogError("Wrong algrithm for week diet calculation!");
	return;
    }

    // Update UI
    var weekDayMap = {
        0: "Sun",
        1: "Mon",
        2: "Tue",
        3: "Wed",
        4: "Thu",
        5: "Fri",
        6: "Sat"
    };
    for (var i = 0; i < dietArray.length; ++i) {
        var uidCurrentDay = "uidColumn" + weekDayMap[i];
        var bgColor = (i == currentDay) ? "yellow" : "white";
        W3SetUICSS(uidCurrentDay, {"background-color": bgColor});

        var dietMaxIndex = 21; // Total 21 foods
        for (var j = 0; j < dietMaxIndex; ++j) {
            var uidCheckbox = "uidDiet" + String(j + 1) + String(i + 1);
            var isDietTrue = ((1 << j) & dietArray[i]) != 0;
            var isDietChecked = W3IsUIChecked(uidCheckbox);
            if (isDietTrue != isDietChecked) {
                W3ClickUI(uidCheckbox);
            }
        }
    }

    W3DisableUI("uidDietUpdateButton");
    W3SetUIText("uidInvisibleDate",
                String(weekDays[0].getFullYear()) + "-" +
                String(weekDays[0].getMonth() + 1) + "-" +
                String(weekDays[0].getDate()));
    W3SetUIText("uidInvisibleOwner", String(personID));
}

function EJUpdateDietForWeek() {
    // Validate datetime format "YYYY-MM-DD"
    var dateString = W3GetUIText("uidInvisibleDate");
    var date = new Date(dateString);
    for (var i = 0; i < 7; ++i) {
        var diet = 0;
        var dietMaxIndex = 21; // Total 20 foods
        for (var j = 0; j < dietMaxIndex; ++j) {
            var uidCheckbox = "uidDiet" + String(j + 1) + String(i + 1);
            var isDietChecked = W3IsUIChecked(uidCheckbox);
            if (isDietChecked) {
                diet |= 1 << j;
            }
        }

        dateString = String(date.getFullYear()) + "-" +
            String(date.getMonth() + 1) + "-" +
            String(date.getDate());

        var aid = "aidUpdateDiet";
        var personID = parseInt(W3GetUIText('uidInvisibleOwner'));
        var request = W3CreateAPI(aid, personID, dateString, diet, W3GetSession());
        W3CallAPISync(request, function(data, status) {});

        date.setDate(date.getDate() + 1);
    }

    W3DisableUI("uidDietUpdateButton");
}

function EJOnDietChecked() {
    var dateString = W3GetUIText("uidInvisibleDate");
    var person = W3GetUIText("uidInvisibleOwner");
    if ((dateString.length > 0) && (person.length > 0)) {
        W3EnableUI("uidDietUpdateButton");
    }
}

function EJMenuDisplay(menuID, menuCount) {
    for (var i = 0; i < menuCount; ++i) {
        if (i == menuID) {
            W3DisplayUI("uidMenuStuff" + String(i));
            W3DisplayUI("uidMenuOperation" + String(i));
            W3DisplayUI("uidMenuTips" + String(i));
        } else {
            W3HideUI("uidMenuStuff" + String(i));
            W3HideUI("uidMenuOperation" + String(i));
            W3HideUI("uidMenuTips" + String(i));
        }
    }
}
